package ru.compscicenter.databases.zaikina.common.commands;


import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import ru.compscicenter.databases.zaikina.common.commands.bodies.DocumentsBody;
import ru.compscicenter.databases.zaikina.common.commands.bodies.StringListBody;
import ru.compscicenter.databases.zaikina.common.model.Document;

import java.util.ArrayList;

public class DocumentsCommand extends SysCommand<ArrayList<Document>, DocumentsBody> {
    public DocumentsCommand(Integer type, DocumentsBody body, Integer param) {
        super(type, body, param);
    }

    public static DocumentsCommand parse(String jsonStr) {
        JSONObject obj = (JSONObject) JSONValue.parse(jsonStr);
        if (SysCommandType.isDocumentsCommand(SysCommand.parseType(obj))) {
            Integer type = Integer.parseInt(obj.get("type").toString());
            Integer param = Integer.parseInt(obj.get("param").toString());
            DocumentsBody body = new DocumentsBody();
            body.getFromJsonString(obj.get("body").toString());
            return new DocumentsCommand(type, body, param);
        }
        return null;
    }
}
